package ch.unisi.inf.pfii.teamwhite.risk.core;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.util.ArrayList;

import ch.unisi.inf.pfii.teamwhite.risk.buildings.Building;
import ch.unisi.inf.pfii.teamwhite.risk.buildings.EmptySlot;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Player;
import ch.unisi.inf.pfii.teamwhite.risk.core.ships.Frigate;

//This class represents a planet on the galaxy which contains a list of neighbours a building and some ships
public final class Planet {

	private final String name;
	private String displayedName;
	
	private Player owner;
	private int moneyPerTurn;
	
	private Building building;
	private ArrayList<Frigate> ships;

	private Boolean isCapital;
	private ArrayList<Planet> neighbours;
	private String region;
	
	public Planet(final String name, final Player owner, final int money) {
		this.name = name;
		this.displayedName = name;
		this.owner = owner;
		moneyPerTurn = money;
		ships = new ArrayList<Frigate>();
		building = null;
		isCapital = false;
		neighbours = new ArrayList<Planet>();
		region = "";
	}

//	Getters and setters
	public final String getName() {
		return name;
	}

	public final String getDisplayedName() {
		return displayedName;
	}

	public final void setDisplayedName(final String name) {
		this.displayedName = name;
	}
	
	public final Player getOwner() {
		return owner;
	}
	
	public final void setOwner(final Player owner) {
		this.owner = owner;
	}
	
	public final int getMoney() {
		return moneyPerTurn;
	}

	public final void setMoney(final int money) {
		moneyPerTurn = money;
	}

	public final Building getBuilding() {
		if (this.building == null) {
			this.building = new EmptySlot();
		}
		return building;
	}

	public final void addBuilding(final Building building) {
		this.building = building;
	}
	
	public final ArrayList<Frigate> getShips() {
		return ships;
	}

	public final void createShip() {
		Frigate ship = new Frigate();
		ships.add(ship);
	}
	
	private final void setShips(final ArrayList<Frigate> array) {
		ships = array;
	}

	public final void resetShips() {
		setShips(new ArrayList<Frigate>());
	}
	
	public final Boolean isCapital() {
		return isCapital;
	}
	
	public final void setCapital(final Boolean isCapital) {
		this.isCapital = isCapital;
	}

	public final ArrayList<Planet> getNeighbours() {
		return neighbours;
	}

	public final void setNeighbours(final ArrayList<Planet> neighbours) {
		this.neighbours = neighbours;
	}
	
	public final String getRegion() {
		return region;
	}
	
	public final void setRegion(final String region) {
		this.region = region;
	}

//	This method returns a String which contains the names of all the neighbours
	private final String neighboursToString() {
		String result = "";
		for (Planet neighbour : neighbours) {
			if (neighbour == null) {
			}
			result = result + neighbour.getName() + " ";
		}
		return result;
	}

	//TODO
//  This method print trough the console the fields of the planet
	public final String getPlanetInfo() {
		String toPrint = "";
		if (getBuilding().getName().equals("")) {
			toPrint = "No buildings";
		} else {
			toPrint = "The building is a " + building.getName();
		}
		return ("This is the planet " + getName() + ", Owned by "
				+ getOwner().getName() + "." + "\n" + "\t"
				+ "Its neighbours are: " + neighboursToString() + "\n" + "\t"
				+ "The turn income is " + getMoney() + "\n" + "\t" + toPrint
				+ "\n" + "\t" + "In this planet there are " + getShips().size() + " ships");
	}
}
